package system.register.impl;


import system.register.ServerInterface;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

/**
 * Created by 陈文杰 on 2022/4/12 19:11
 */

/*
1.接收客户端发送的数据
2.处理客户端发送的数据
3.发送处理数据给客户端
 */
public class ServerImpl extends Thread implements ServerInterface {


    @Override
    public void run() {
        response();
    }

    //设置静态的Socket对象,方便方法使用
    private static ServerSocket serverSocket = null;
    private static Socket socket = null;
    private static int errorNum = 0;

    //设置验证令牌
    private int token = 1;

    //接收客户端数据,验证用户信息
    @Override
    public String verify() {
        //用于接收客户端的信息
        StringBuilder stringBuilder = new StringBuilder();
        String[] strings = null;

        //记录管理员名称,密码
        String Administrator = null;
        String AdministratorPassword = null;

        boolean isTrue = false;
        //调用ServerSocket对象,监视8888端口
        try {
            if (serverSocket == null) serverSocket = new ServerSocket(8888);


        } catch (IOException e) {
            e.printStackTrace();
        }

        //阻塞等待8888端口连接
        try {
            socket = serverSocket.accept();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //创建和socket相关的输入流对象
        InputStream inputStream = null;
        try {
            inputStream = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //读取从客户端接收到的信息
        byte[] bytes = new byte[1024];
        int readLines = 0;
        while (true) {
            try {
                readLines=inputStream.read(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (readLines==-1){
                break;
            }
            stringBuilder.append(new String(bytes, 0, readLines));
        }

        //将信息分为用户名,密码
        strings = stringBuilder.toString().split("/");

        //查看数据是否正确,并返回结果
        Administrator = getProperties("Administrator");
        System.out.println(Administrator);
        AdministratorPassword = getProperties("AdministratorPassword");
        isTrue = strings[0].equals(Administrator) && strings[1].equals(AdministratorPassword);
        if (strings[0].equals(Administrator) && strings[1].equals(AdministratorPassword)) {
            return "管理员";
        }
        //若管理员验证失败,则再次验证是否为收银员
        Administrator = getProperties("Cashier");
        AdministratorPassword = getProperties("CashierPassword");
        isTrue = strings[0].equals(Administrator) && strings[1].equals(AdministratorPassword);
        if (strings[0].equals(Administrator) && strings[1].equals(AdministratorPassword)) {
            return "收银员";
        }
        return "查无此人";

    }

    //向客户端发送验证结果消息
    public void response() {
        String type = null;//接收返回结果
        type = verify();
        //获取和socket相关的输出流
        OutputStream outputStream = null;
        try {
            outputStream = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //向客户端发送用户信息是否正确的结果
        if (type.equals("管理员")) {
            try {
                outputStream.write("管理员".getBytes());
                socket.shutdownOutput();//通知客户端信息发送完毕
            } catch (IOException e) {
                e.printStackTrace();
            }

            return;
        }
        if (type.equals("收银员")) {
            try {
                outputStream.write("收银员".getBytes());
                socket.shutdownOutput();//通知客户端信息发送完毕
            } catch (IOException e) {
                e.printStackTrace();
            }

            return;
        }
        if (type.equals("查无此人")) {
            try {
                outputStream.write("查无此人".getBytes());
                socket.shutdownOutput();//通知客户端信息发送完毕
                errorNum++;
                if (errorNum < 3) response();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /*
    从配置文件中获取管理员名称和密码
    根据type类型选择返回名称还是密码
     */
    private String getProperties(String type) {
        Properties properties = new Properties();
        InputStreamReader inputStream = null;
        try {

//            inputStream = new InputStreamReader(ServerImpl.class.getClassLoader().getResourceAsStream("system.properties"), "utf-8");
            inputStream = new InputStreamReader(ServerImpl.class.getClassLoader().getResourceAsStream("system.properties"));
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (type.equals("Administrator")) return properties.getProperty("Administrator");
        if (type.equals("AdministratorPassword")) return properties.getProperty("AdministratorPassword");
        if (type.equals("Cashier")) return properties.getProperty("Cashier");
        if (type.equals("CashierPassword")) return properties.getProperty("CashierPassword");
        return null;

    }
}
